package com.bignerdranch.android.nerdlauncher;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by ruben.gonzalez on 28/05/14.
 */
public class NerdLauncherFragment extends ListFragment {
    private static final String TAG = "NerdLauncherFragment";
    private List<ResolveInfo> mActivities;
    private PackageManager mPm;

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ResolveInfo info = (ResolveInfo) l.getAdapter().getItem(position);
        ActivityInfo activityInfo = info.activityInfo;

        if(activityInfo == null) return ;

        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setClassName(activityInfo.applicationInfo.packageName, activityInfo.name);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent startupIntent = new Intent(Intent.ACTION_MAIN);
        startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mPm = getActivity().getPackageManager();
        mActivities = mPm.queryIntentActivities(startupIntent, 0);

        Log.i(TAG, "I've found " + mActivities.size() + " activities");

        Collections.sort(mActivities, new Comparator<ResolveInfo>() {
            @Override
            public int compare(ResolveInfo a, ResolveInfo b) {

                return String.CASE_INSENSITIVE_ORDER.compare(
                        a.loadLabel(mPm).toString(), b.loadLabel(mPm).toString()
                );
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_list_fragment, container,
                false);

        ListView listView = (ListView) v.findViewById(android.R.id.list);
        AppAdapter adapter = new AppAdapter(mActivities);
        listView.setAdapter(adapter);

        return v;
    }

    private class AppAdapter extends ArrayAdapter<ResolveInfo>{

        public AppAdapter(List<ResolveInfo> list) {
            super(getActivity(), 0, list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // If we weren't given a view, inflate one
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(
                        R.layout.list_item_app, null);
            }

            // Configure the view for this crime
            ResolveInfo c = getItem(position);

            TextView title = (TextView) convertView
                    .findViewById(R.id.app_list_item_title);
            title.setText(c.loadLabel(mPm));
            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.app_list_item_imageView);
            imageView.setImageDrawable(c.loadIcon(mPm));

            return convertView;
        }
    }
}
